package com.itheima.server.service;

import com.itheima.api.FriendApi;
import com.itheima.api.UserApi;
import com.itheima.api.UserInfoApi;
import com.itheima.autoconfig.template.HuanXinTemplate;
import com.itheima.domain.Friend;
import com.itheima.domain.User;
import com.itheima.domain.UserInfo;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageResult;
import com.itheima.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Resource
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    /**
     * 根据环信id查询用户信息
     * @param hxId
     * @return
     */
    public UserInfoVo getUserInfo(String hxId) {
        //根据环信id查到user对象
        User user = userApi.getByHxId(hxId);
        //根据userid查
        UserInfo userInfo = userInfoApi.getById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    /**
     * 联系人添加
     * @param userId
     */
    public void contacts(long userId) {
        Long curId = UserHolder.getUserId();
        //查环信账号
        User user = userApi.findById(userId);
        User cur = userApi.findById(curId);
        //讲好友关系注册到环信
        Boolean flag = huanXinTemplate.addContact(user.getHxUser(), cur.getHxUser());
        if (flag){
            //注册成功,将好友关系添加到MongoDB
            friendApi.save(cur.getId(),user.getId());
        }
    }


    /**
     * 查询好友列表
     * @param keyword
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult friendList(String keyword, Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();
        //查询好友列表
        List<Friend> friendLists = friendApi.friendList(userId,page,pagesize);
        if (friendLists == null){
            //这里可以进行判断，如果为空就去查所有好友列表，不为空就分页查
            return new PageResult();
        }
        //获取好友用户id
        List<Long> userIds = friendLists.stream().map(Friend::getFriendId).collect(Collectors.toList());
        //查询userInfo
        UserInfo temp = new UserInfo();
        temp.setNickname(keyword);
        Map<Long, UserInfo> userInfos = userInfoApi.getByIds(userIds, temp);
        //封装vo对象
        List<ContactVo> list = new ArrayList<>();
        for (Friend friend : friendLists) {
            UserInfo userInfo = userInfos.get(friend.getFriendId());
            if (userInfo != null){
                ContactVo vo = ContactVo.init(userInfo);
                list.add(vo);
            }
        }
        return new PageResult(page,pagesize,0,list);
    }

    /**
     * 删除联系人
     */
    public void deleteFriend(Long friendId) {
        Long userId = UserHolder.getUserId();
        //获取环信账号
        User friend = userApi.findById(friendId);
        User user = userApi.findById(userId);
        //环信删除好友
        Boolean flag = huanXinTemplate.deleteContact(user.getHxUser(), friend.getHxUser());
        //去mongo里把好友删了
        if (flag){
            friendApi.delete(userId,friendId);
        }
    }


}
